/*
 *
 *       Functions, Object to process the collections, set, subsets ....
 *
 **/

/*
 *       @function
 *       @return true: the element x in X
 * */
var set_in = function(x, X, fn){
    if (!fn){
        fn = function(a, b){
            return a == b;
        }
    }
    for(var i = 0; i < X.length; i++){
        if (fn(x, X[i])){
            return true;
        }
    }
    return false;
}
module.exports.set_in = set_in;

/*
 *
 *       @function
 *       @return union of two sets X, Y
 *       {1, 2, 3} ∪ {3, 4, 5} = {1, 2, 3, 4, 5}
 *
 * */
module.exports.set_union = function(X, Y, fn){
    if (!fn){
        fn = function(a, b){
            return a == b;
        }
    }
    var ret = Y.slice(0, Y.length);
    for(var i = 0; i < X.length; i++){
        if (!set_in(X[i], Y, fn)){
            ret.push(X[i]);
        }
    }
    return ret;
}

/*
 *
 *       @function
 *       @return intersection of two sets X, Y
 *       {1, 2} ∩ {2, 3} = {2}
 *
 * */
module.exports.set_intersection = function(X, Y, fn){
    if (!fn){
        fn = function(a, b){
            return a == b;
        }
    }
    var ret = [];
    for(var i = 0; i < X.length; i++){
        if (set_in(X[i], Y, fn)){
            ret.push(X[i]);
        }
    }
    return ret;
}

/*
 *       @function
 *       @return complement of set X with set Y
 *       {1, 2, 3, 4} \ {1, 3} = {2, 4}
 *
 */
module.exports.set_complement = function(X, Y, fn){
    if (!fn){
        fn = function(a, b){
            return a == b;
        }
    }
    var ret = [];
    for(var i = 0; i < X.length; i++){
        if (!set_in(X[i], Y, fn)){
            ret.push(X[i]);
        }
    }
    return ret;
}
module.exports.intersection = function(X, Y){
    var ret = 0;
    for(var i = 0; i< X.length; i++){
        for(var j = 0; j< Y.length; j++){
            if(X[i].mColor == Y[j].mColor && X[i].mRank == Y[j].mRank){
                ret++
            }
        }
    }
    if(ret == X.length){
        return true
    }
    else{
        return false;
    }
}

